package com.jzo2o.customer.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.impl.AddressBookServiceImpl;
import com.jzo2o.mvc.config.AutoConfiguration;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api("tags=地址铺相关接口")
public class AddressBookController {
    @Resource
    private AddressBookServiceImpl addressBookService;



    //新增地址铺接口
    @PostMapping
    @ApiOperation("地址铺新增")
    public void add(@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.add(addressBookUpsertReqDTO);
    }
    @GetMapping("/page")
    @ApiOperation("分页查询地址铺列表")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO) {
        return addressBookService.page(addressBookPageQueryReqDTO);
    }
    @PutMapping("/default")
    @ApiOperation("地址铺默认地址设置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址薄id", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "flag", value = "是否为默认地址，0：否，1：是", required = true, dataTypeClass = Integer.class)
    })
    public void setDefault(
            @NotNull (message="id不能为空") @RequestParam("id") Long id,
            @NotNull (message="flag不能为空") @RequestParam("flag") Integer flag
    ) {
        Long userId= UserContext.currentUserId();
        addressBookService.setDefault(userId,id,flag);
    }
    @GetMapping("/{id}")
    @ApiOperation("地址铺详情")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "地址薄id", required = true, dataTypeClass = Long.class)})
    public AddressBookResDTO getById(@PathVariable("id") Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return BeanUtil.toBean(addressBook, AddressBookResDTO.class);
    }
    @PutMapping("/{id}")
    @ApiOperation("地址铺修改")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "地址薄id", required = true, dataTypeClass = Long.class)})
    public void update(@PathVariable("id") Long id, @RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        addressBookService.update(id, addressBookUpsertReqDTO);
    }
    @DeleteMapping("/batch")
    @ApiOperation("地址铺删除")
    @ApiImplicitParams({ @ApiImplicitParam(name = "ids", value = "地址薄id列表", required = true, dataTypeClass = List.class)})
    public void delete(@NotNull (message="ids不能为空") @RequestBody List<Long>  ids) {
        addressBookService.removeByIds(ids);
    }
    @GetMapping("defauleAddress")
    @ApiOperation("获取默认地址")
    public AddressBookResDTO getDefaultAddress() {
        Long userId= UserContext.currentUserId();
       return addressBookService.getDefaultAddress(userId);
    }
}
